/**
 * File: PageParser.java
 * TCSS 422 Team Project 1
 * Autumn 2009
 * Team Members: Darin Cyr, Kirk Leonard, John Patanian
 */

package singlethread;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;

/**
 * Handles all the parsing of web pages
 * Takes pages from the page buffer and looks for links.
 * Links found are added to the URLBucket
 * Refer to http://www.informit.com/articles/article.aspx?p=31059
 * @author Darin Cyr
 * @author Kirk Leonard
 * @author John Patanian
 * @version Autumn 2009
 */
public class PageParser extends HTMLEditorKit.ParserCallback 
{
  /**
  * The url.
  */
  private URL my_url;
  
  /**
  * The store for urls.
  */
  private URLBucket my_url_list;
  
  /**
   * Used for naming files.
   * 
   * */
  private int my_page_count;
  
  /**
   * Used for printing out the results.
   */
  private PrintStream my_output;

  /**  The class that handles all the parsing of web pages
   * it takes a reference URLBucket to store urls in, the current url, and
   * a reference to pagecount, which is used to generate filenames.
   * @param the_url_list The list of urls to parse
   * @param the_url The base url of the current file being parsed.
   * @param the_page_count used to generate filenames.
   * */
  public PageParser(final URLBucket the_url_list, final URL the_url,
  final int the_page_count)
  {
    my_url = the_url;
    my_url_list = the_url_list;
    my_page_count = the_page_count;
  
    openFile();
  }

  /**
  * what to do with text.
  * text comes in bunches, so open a file once, and add text as it's found
  * @param the_text The text.
  * @param the_pos The position.
  */
  public void handleText(final char [] the_text, final int the_pos)
  {
    for (int i = 0; i < the_text.length; i++)
    {
      my_output.print(the_text[i]);
    }
    
    my_output.println();

  }

  /**
  * @see javax.swint.text.html.HTMLEditorKit$ParserCallback#handleSimpleTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, int)
  * @param the_tag The html tag
  * @param the_attribute_set The html attribute set
  * @param the_pos The position.
  */
  public void handleSimpleTag(final HTML.Tag the_tag, 
    final MutableAttributeSet the_attribute_set,
    final int the_pos)
  {
    String href = (String) the_attribute_set.getAttribute(HTML.Attribute.HREF);
      
    if (href == null && the_tag.equals(HTML.Tag.FRAME))
    {
      href = (String) the_attribute_set.getAttribute(HTML.Attribute.SRC);
    }
      
    if (href == null || href.toLowerCase().startsWith("mailto:"))
    {
      return;
    }

    final int location = href.indexOf('#');
      
    if (location != -1)
    {
      href = href.substring(0, location);
    }
      
    handleLink(my_url, href);
  }


  /**
  * Simple tags are tags like <br> that do not have opening and closing tags.
  * Start tags are the first tags of pairs.
  * The <p> in a <p></p> pair, for example
  * @param the_tag The html tag.
  * @param the_attribute_set The attribute set.
  * @param the_pos The position.
  */
  public void handleStartTag(final HTML.Tag the_tag, 
    final MutableAttributeSet the_attribute_set,
    final int the_pos)
  {
    handleSimpleTag(the_tag, the_attribute_set, the_pos);    // handle the same way
  }

    
  /**
   * This handles when a url is found.
   * @param the_base_url The base url.
   * @param the_spec The spec
   */
  protected void handleLink(final URL the_base_url, final String the_spec)
  {
    URL a_url = null;
    
    try
    {
      a_url = new URL(the_base_url, the_spec);
    }
    catch (final MalformedURLException e)
    {
    }

    if (a_url != null)
    {
      my_url_list.addURL(a_url);
    }

  }
    
    /**
     * Cache the file locally for later processing.
     */
  public void openFile()
  {

    try
    {
      my_output = new PrintStream(new File("file" + my_page_count));
    }
    catch (final FileNotFoundException e)
    {
      //don't do anything
    }
    my_output.println("text for page: " + my_url);
  }
}
